﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Input;
using DAO;
using Entidades;
using LisBioGUIWPF.Controls.User;

namespace LisBioGUIWPF.Controls
{
    /// <summary>
    /// Lógica de interacción para InvestigadorControl.xaml
    /// </summary>
    public partial class InvestigadorControl : System.Windows.Controls.UserControl
    {
        private LisBio lisBio;
        private UserData.Operacion oper;

        public InvestigadorControl(LisBio lisBio, bool asistencia)
        {
            InitializeComponent();
            this.lisBio = lisBio;
            this.lblOperacion.Content = "";

            if (asistencia)
            {
                this.btnUser.Visibility = Visibility.Hidden;
                this.btnDelete.Visibility = Visibility.Hidden;
                this.btnModify.Visibility = Visibility.Hidden;

                this.Loaded += new RoutedEventHandler(InvestigadorControl_Loaded);
            }
        }

        void InvestigadorControl_Loaded(object sender, RoutedEventArgs e)
        {
            this.btnDelete_Click(this.btnAsistencia, null);
        }

        private void btnUser_Click(object sender, RoutedEventArgs e)
        {
            this.gridBuscar.Visibility = Visibility.Hidden;
            this.lblOperacion.Content = "";

            UserData userData = new UserData(lisBio, UserData.Operacion.NUEVO, this);
            userData.txtNombre.Focus();
            this.IsEnabled = false;
            this.lisBio.borderHuella.Child = userData;
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            if (sender == this.btnDelete)
            {
                oper = UserData.Operacion.ELIMINAR;
                this.lblOperacion.Content = "Operación Eliminar";
            }
            else if (sender == this.btnModify)
            {
                oper = UserData.Operacion.MODIFICAR;
                this.lblOperacion.Content = "Operación Modificar";
            }
            else
            {
                oper = UserData.Operacion.ASISTENCIA;
                this.lblOperacion.Content = "Operación Asistencia";
            }

            this.gridBuscar.Visibility = Visibility.Visible;
            ImageControl image = new ImageControl(this.lisBio);
            image.setDefaultImage();
            this.lisBio.borderHuella.Child = image;

            this.listarInvestigadores();
            this.comboBuscar.Focus();
        }

        private void btnBuscar_Click(object sender, RoutedEventArgs e)
        {
            if (comboBuscar.SelectedItem != null)
            {
                UserData userData = new UserData(lisBio, oper, this);
                userData.txtNombre.Focus();

                EntidadInvestigador datosInvestigador;
                datosInvestigador = (EntidadInvestigador)comboBuscar.SelectedItem;

                //Cargando la entidad seleccionada al userData para que cargue los datos en pantalla
                userData.Usuario = datosInvestigador;
                userData.cargarDatos();

                this.IsEnabled = false;
                this.lisBio.borderHuella.Child = userData;
            }
        }

        private void comboBuscar_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key != System.Windows.Input.Key.Escape &&
                e.Key != System.Windows.Input.Key.Down &&
                e.Key != System.Windows.Input.Key.Up &&
                e.Key != System.Windows.Input.Key.Left &&
                e.Key != System.Windows.Input.Key.Right &&
                e.Key != System.Windows.Input.Key.Enter)
            {

                this.comboBuscar.IsDropDownOpen = true;
            }
            else if (e.Key == Key.Enter)
            {
                this.btnBuscar_Click(sender, null);
            }

            this.lblOperacion.Content = "";
        }

        /*
         * Lista los investigadores en el comboBuscar de acuerdo al radioButton que este seleccionado. Si se encuentra
         * seleccionada la busqueda por Nombre muestra el nombre, apellido y el legajo, si la busqueda es por legajo se
         * muestra primero el legajo seguido del nombre.
         */

        public void listarInvestigadores()
        {
            try
            {
                comboBuscar.Items.Clear();

                List<EntidadBase> entidades = new List<EntidadBase>();
                InvestigadorDAO daoInvestigador = new InvestigadorDAO();

                entidades = daoInvestigador.leerTodas();
                for (int i = 0; i < entidades.Count; i++)
                {
                    EntidadInvestigador investigador;
                    investigador = (EntidadInvestigador)entidades[i];
                    if (!(bool)investigador.EsEncargado && investigador.Activo == true)
                    {
                        comboBuscar.Items.Add(investigador);
                    }
                }

                if ((bool)radioNombre.IsChecked)
                {
                    comboBuscar.DisplayMemberPath = "NombreYLegajo";
                }
                else
                {
                    comboBuscar.DisplayMemberPath = "LegajoYNombre";
                }
                comboBuscar.SelectedValuePath = "Id";
            }
            catch (Exception exc) { }
        }

        /*
        private void comboBuscar_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            int index;
            string actual;
            string found;

            // Do nothing for certain keys, such as navigation keys.
            if ((e.KeyCode == Keys.Back) ||
            (e.KeyCode == Keys.Left) ||
            (e.KeyCode == Keys.Right) ||
            (e.KeyCode == Keys.Up) ||
            (e.KeyCode == Keys.Down) ||
            (e.KeyCode == Keys.Delete) ||
            (e.KeyCode == Keys.PageUp) ||
            (e.KeyCode == Keys.PageDown) ||
            (e.KeyCode == Keys.Home) ||
            (e.KeyCode == Keys.End))
            {
                return;
            }

            // Store the actual text that has been typed.
            actual = this.comboBuscar.Text;

            // Find the first match for the typed value.
            index = this.comboBuscar.FindString(actual);

            // Get the text of the first match.
            if (index > -1)
            {
                found = this.comboBuscar.Items[index].ToString();

                // Select this item from the list.
                this.comboBuscar.SelectedIndex = index;

                // Select the portion of the text that was automatically
                // added so that additional typing replaces it.
                this.comboBuscar.SelectionStart = actual.Length;
                this.comboBuscar.SelectionLength = found.Length;
            }
        }
        */

        private void radio_Checked(object sender, RoutedEventArgs e)
        {
            listarInvestigadores();
        }
            
    }
}
